package com.lqq.chapter04.HashSetDemo;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author lqq677
 * @Date 2021/8/10 16:01
 * @Description 解决HashSet线程不安全方案：CopyOnWriteArraySet(写时复制)
 */
public class CopyOnWriteArraySetTest {
    public static void main(String[] args) {
        Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                //向集合添加内容
                set.add(UUID.randomUUID().toString().substring(0, 8));
                //从集合里获取内容
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
